﻿$ErrorActionPreference = "Stop"
$msbuild = "C:\Windows\Microsoft.NET\Framework\v4.0.30319\msbuild.exe"

Write-Host "1. Clean Packages"
$binDir = '.\*\bin\Release'
$bin = '.\*\bin'
Get-ChildItem -Path $binDir -Filter *.nupkg -Recurse | foreach ($_) {remove-item $_.fullname -Force}
Remove-Item $bin -Recurse -Force

# Write-Host "2. PowerShell"
# $ClientWebsite = '.\Sustainalytics.ClientInterface.Service\bin\Release\website'
# $AdminWebsite = '.\Sustainalytics.AdminInterface.Service\bin\Release\website'
# $ClientWebsiteNuspec = '.\Sustainalytics.ClientInterface.Service\Sustainalytics.ClientInterface.Service.nuspec'
# $AdminWebsiteNuspec = '.\Sustainalytics.AdminInterface.Service\Sustainalytics.AdminInterface.Service.nuspec'

# Remove-Item -Recurse -Force $ClientWebsite
# Remove-Item -Recurse -Force $AdminWebsite
# Remove-Item -Force $ClientWebsiteNuspec
# Remove-Item -Force $AdminWebsiteNuspec

Write-Host "4. Install Grunt Client"
Set-Location .\Sustainalytics.ClientInterface.Node
Invoke-Expression "npm install"
# & 'npm' "install"
Set-Location ..\

Write-Host "5. Install Grunt Admin"
Set-Location .\Sustainalytics.AdminInterface.Node
Invoke-Expression "npm install"
Set-Location ..\

Write-Host "6. Generate app.js"
Set-Location .\Sustainalytics.Core.Node
$project = "Sustainalytics.Core.Node.njsproj"
$params = "/t:Build"
Invoke-Expression "$msbuild $project $params"

Write-Host "7. Node.js"
Invoke-Expression "node app.js"
Set-Location ..\

Write-Host "8. Make sure bundles directory exists"
$Admin_bundles = '.\Sustainalytics.AdminInterface.UI\modules\bundles'
$Client_bundles = '.\Sustainalytics.ClientInterface.UI\modules\bundles'
if (!(Test-Path $Admin_bundles)) {
	New-Item -ItemType directory -Path $Admin_bundles
}

if (!(Test-Path $Client_bundles)) {
	New-Item -ItemType directory -Path $Client_bundles
}

Write-Host "9. Build ClientPlatform.UI"
$project = "_UI.sln"
Invoke-Expression "$msbuild $project"
if (! $?) { Exit $LastExitCode }

Write-Host "10. Client Interface gruntfile.js"
Set-Location .\Sustainalytics.ClientInterface.Node
$project = "Sustainalytics.ClientInterface.Node.csproj"
Invoke-Expression "$msbuild $project"
if (! $?) { Exit $LastExitCode }

Write-Host "11. Build Client Interface UI"
Invoke-Expression "grunt"
Set-Location ..\

Write-Host "12. Client Modules"
Set-Location .\Sustainalytics.ClientInterface.UI
$project = "Sustainalytics.ClientInterface.UI.csproj"
Invoke-Expression "$msbuild $project"
if (! $?) { Exit $LastExitCode }
Set-Location ..\

Write-Host "13. Admin Interface gruntfile.js"
Set-Location .\Sustainalytics.AdminInterface.Node
$project = "Sustainalytics.AdminInterface.Node.csproj"
Invoke-Expression "$msbuild $project"
if (! $?) { Exit $LastExitCode }
Set-Location ..\

Write-Host "14. Build Admin Interface UI"
Set-Location .\Sustainalytics.AdminInterface.Node
Invoke-Expression "grunt"
Set-Location ..\

Write-Host "15. Admin Modules"
Set-Location .\Sustainalytics.AdminInterface.UI
$project = "Sustainalytics.AdminInterface.UI.csproj"
Invoke-Expression "$msbuild $project"
if (! $?) { Exit $LastExitCode }
Set-Location ..\

Write-Host "16. Packaged GAP Client Service"
Set-Location .\Sustainalytics.ClientInterface.Service
$project = "Sustainalytics.ClientInterface.Service.csproj"
Invoke-Expression "$msbuild $project"
if (! $?) { Exit $LastExitCode }
Set-Location ..\

Write-Host "17. Packaged GAP Admin Service"
Set-Location .\Sustainalytics.AdminInterface.Service
$project = "Sustainalytics.AdminInterface.Service.csproj"
Invoke-Expression "$msbuild $project"
if (! $?) { Exit $LastExitCode }
Set-Location ..\
